package meijuli.com.join51.business.ui;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.SimpleAnimatorListener;

public class OneKeyDeleteEditView extends RelativeLayout {
    private OnInputViewActionListener mOnInputViewActionListener;
    EditText inputEditText;
    View cancelView;
    /** 默认提示内容 */
    private String mHintContent;

    public OneKeyDeleteEditView(Context context) {
        super(context);
        init(null);
    }

    public OneKeyDeleteEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public OneKeyDeleteEditView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        View view = inflate(getContext(), R.layout.one_key_delete_edit_view, this);
        setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
        inputEditText = view.findViewById(R.id.inputEditText);
        cancelView = view.findViewById(R.id.cancelView);
        if (attrs != null) {
            TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs,
                    R.styleable.OneKeyDeleteEditView, 0, 0);
            mHintContent = ta.getString(R.styleable.OneKeyDeleteEditView_OneKeyHint);
            int maxLength = ta.getInteger(R.styleable.OneKeyDeleteEditView_OneKeyMaxLength,
                    Integer.MAX_VALUE);
            ta.recycle();

            setHint(mHintContent);
            setMaxLength(maxLength);
        }
        cancelView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                inputEditText.setText("");
                if (!TextUtils.isEmpty(mHintContent)) {
                    inputEditText.setHint(mHintContent);
                }
                if (null != mOnInputViewActionListener) {
                    mOnInputViewActionListener.onAction(OnInputViewActionListener.ACTION_CLEAR_INPUT);
                }
            }
        });
        inputEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = inputEditText.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    if (cancelView.getVisibility() == View.VISIBLE) {
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(500);
                        animatorSet.playTogether(ObjectAnimator.ofFloat(cancelView, "scaleX", 1f, 0f), ObjectAnimator.ofFloat(cancelView, "scaleY", 1f, 0f));
                        animatorSet.addListener(new SimpleAnimatorListener() {
                            @Override
                            public void onAnimationEnd(Animator animator) {
                                cancelView.setVisibility(View.INVISIBLE);
                                cancelView.setEnabled(false);
                                if (null != mOnInputViewActionListener) mOnInputViewActionListener.onAction(OnInputViewActionListener.ACTION_CLEAR_INPUT);
                                super.onAnimationEnd(animator);
                            }
                        });
                        animatorSet.start();
                        cancelView.setEnabled(false);
                    }
                } else {
                    if (cancelView.getVisibility() == View.INVISIBLE) {
                        cancelView.setEnabled(true);
                        cancelView.setVisibility(View.VISIBLE);
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(500);
                        cancelView.setVisibility(View.VISIBLE);
                        animatorSet.playTogether(ObjectAnimator.ofFloat(cancelView, "scaleX", 0f, 1f), ObjectAnimator.ofFloat(cancelView, "scaleY", 0f, 1f));
                        animatorSet.addListener(new SimpleAnimatorListener() {
                            @Override
                            public void onAnimationEnd(Animator animator) {
                                if (null != cancelView) cancelView.setEnabled(true);
                                super.onAnimationEnd(animator);
                            }
                        });
                        animatorSet.start();
                    }
                }
            }
        });
    }

    public EditText getInputEditText() {
        return inputEditText;
    }

    /**
     * 设置搜索提示
     */
    public void setHint(String hint) {
        if (null != inputEditText) {
            mHintContent = hint;
            inputEditText.setHint(hint);
        }
    }

    /**
     * 设置输入文本类型
     * @param inputType
     */
    public void setInputType(int inputType) {
        if (null != inputEditText) {
            inputEditText.setInputType(inputType);
        }
    }

    /**
     * 设置最大长度
     * @param maxLength
     */
    public void setMaxLength(int maxLength) {
        if (null != inputEditText) {
            inputEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }
    }

    /**
     * 设置输入监听
     * @param textWatcher
     */
    public void setTextChangedListener(TextWatcher textWatcher) {
        if (null != inputEditText) {
            inputEditText.addTextChangedListener(textWatcher);
        }
    }

    /**
     * 获取输入的文本
     * @return
     */
    public String getText() {
        if (null != inputEditText) {
            return inputEditText.getText().toString();
        }
        return null;
    }

    /**
     * 设置文本
     * @param text
     */
    public void setText(String text) {
        if (null != inputEditText) {
            inputEditText.setText(text);
        }
    }

    /**
     * 设置光标所在位置
     * @param start
     * @param stop
     */
    public void setSelection(int start, int stop) {
        if (null != inputEditText) {
            inputEditText.setSelection(start, stop);
        }
    }

    /**
     * 设置错误提示
     * @param error
     */
    public void setError(String error) {
        if (null != inputEditText) {
            inputEditText.setError(error);
        }
    }

    public EditText editText() {
        return inputEditText;
    }

    public void setInputViewActionListener(OnInputViewActionListener onInputViewActionListener) {
        mOnInputViewActionListener = onInputViewActionListener;
    }

    public interface OnInputViewActionListener {
        int ACTION_CLEAR_INPUT = 2;
        void onAction(int action);
    }

}
